package dao;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: HP
 * Date: 2023 -06 -25
 * Time: 20:16
 */

import bean.User;
import util.DBUtil;
import util.OrderSystemException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 1.登录-查询通过name
 * 2.注册-插入
 * 3.展示-查询通过id
 */
public class UserDao {
    public void add(User user) throws OrderSystemException {
        Connection connection = DBUtil.getConnection();
        String sql = "insert into user values(null,?,?,?)";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getUserName());
            statement.setString(2, user.getPassword());
            statement.setInt(3, user.getIsAdmin());
            int ret = statement.executeUpdate();
            if (ret != 1) {
                throw new OrderSystemException("插入用户失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new OrderSystemException("插入用户失败");
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }

    public User selectByName(String userName) throws OrderSystemException {
        Connection connection = DBUtil.getConnection();
        String sql = "select * from user where userName=?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1, userName);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                User user = new User(resultSet.getInt("userId"),
                        resultSet.getString("userName"),
                        resultSet.getString("password"),
                        resultSet.getInt("isAdmin"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new OrderSystemException("按名字查询用户失败");
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    public User selectById(int userId) throws OrderSystemException {
        Connection connection = DBUtil.getConnection();
        String sql = "select * from user where userId=?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                User user = new User(resultSet.getInt("userId"),
                        resultSet.getString("userName"),
                        resultSet.getString("password"),
                        resultSet.getInt("isAdmin"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new OrderSystemException("按id查询用户失败");
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    public static void main(String[] args) throws OrderSystemException {
        // 这个方法中进行 UserDao 的单元测试
        UserDao userDao = new UserDao();
        // 1. 验证插入数据
        User user = new User();
        user.setUserName("汤");
        user.setPassword("123456");
        user.setIsAdmin(0);
        userDao.add(user);
        //2. 验证按照 名字 查找数据
        User user1 = userDao.selectByName("汤");
        System.out.println("按照名字查找");
        System.out.println(user1);

        user1 = userDao.selectById(0);
        System.out.println("按照 id 查找");
        System.out.println(user1);
        user1 = userDao.selectById(1);
        System.out.println("按照 id 查找");
        System.out.println(user1);
    }
}
